﻿namespace RuoVea.Stocks.Web.Controllers;

[Authorize(Roles = "None")]
public partial class RecordController : Controller
{
    /// <summary>
    /// 
    /// </summary>
    private readonly RecordService _recordService;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="recordService"></param>
    public RecordController(RecordService recordService)
    {
        _recordService = recordService;
    }

    public IActionResult Index()
    {
        return View();
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <returns></returns>
    [HttpPost("Record/pages")]
    public async Task<RestfulResult<PageResult<StockContent>>> GetPagesAsync()
    {
        using var sr = new StreamReader(Request.Body);
        string dataStr = await sr.ReadToEndAsync();
        if (Request.Body.CanSeek)
            Request.Body.Position = 0;
        if (string.IsNullOrWhiteSpace(dataStr))
            throw new Exception("未提供上传数据。请在Request.Body中携带上传数据。");
        RecordsPageParam? data = JsonConvert.DeserializeObject<RecordsPageParam>(dataStr);
        data ??= new RecordsPageParam { PageNo = 1, PageSize = 10 };

        return await _recordService.GetPagesAsync(data);
    }

    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [UnitOfWork]
    [HttpPost("Record/add")]
    public async Task<RestfulResult> AddAsync([FromBody] StockContent data)
    {
        return await _recordService.AddAsync(data);
    }

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("Record/update")]
    public async Task<RestfulResult> UpdateAsync([FromBody] StockContent data)
    {
        return await _recordService.UpdateAsync(data);
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpDelete("Record/delete")]
    public async Task<RestfulResult> DeleteAsync([FromBody] EntityBaseId data)
    {
        return await _recordService.DeleteAsync(data);
    }

    /// <summary>
    /// 获取分类
    /// </summary>
    /// <returns></returns>
    [HttpGet("Record/category")]
    public async Task<RestfulResult<object>> GetCategoryAsync()
    {
        return await _recordService.GetCategoryAsync();
    }
}